package com.tom.config;

import java.net.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class ConstantsConfig {
    /*二维码相关数据*/
    public final static Map<String, Map> map = new HashMap();
    /*收银商品数据 */
    public final static Map<String, Map> cashierMap = new HashMap();
    public static final String FILE_UPLOAD_DIR="D://uploadFile";
    public static final String INPUT_NET_IP =getIntranetIp();
    public static final String OUTER_NET_IP = "http://"+getInternetIp();
    public static final String PORT = "8089";


    /*日期工具类常量*/
    public final static int CUT_OUT = 4;
    public final static int EVERYDAY_SECOND = 86400;
    public final static int MINUS_1 = -1;
    public final static int MINUS_7 = -7;
    public final static int MINUS_15 = -15;
    public final static int MINUS_3 = -3;
    public final static int MINUS_6 = -6;


    /**
     * 获得内网IP
     * @return 内网IP
     */
    private static String getIntranetIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获得外网IP
     * @return 外网IP
     */
    private static String getInternetIp() {
        try {
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            Enumeration<InetAddress> adds;
            while(networks.hasMoreElements()){
               adds = networks.nextElement().getInetAddresses();
              while (adds.hasMoreElements()){
                  ip = adds.nextElement();
                  if (ip != null
                      && ip instanceof Inet4Address
                      && ip.isSiteLocalAddress()
                      && !ip.getHostAddress().equals(INPUT_NET_IP)){
                  return ip.getHostAddress();
                  }
              }
            }
            // 如果没有外网IP，就返回内网IP
            return INPUT_NET_IP;
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
    }


}
